package com.gaohe.web.controller.dcp;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.core.page.TableDataInfo;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.dcp.domain.DcpPatient;
import com.gaohe.dcp.domain.DcpScale;
import com.gaohe.dcp.domain.DcpScaleScreen;
import com.gaohe.dcp.service.IDcpPatientService;
import com.gaohe.dcp.service.IDcpResultService;
import com.gaohe.dcp.service.IDcpScaleScreenService;
import com.gaohe.dcp.service.IDcpScaleService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

// 定义一个包含 scaleId 和 patientId 的类
class ScalePatientAssignment {
    @JsonProperty("scaleId")
    private ArrayList<Long> scaleId;

    @JsonProperty("patientId")
    private ArrayList<Long> patientId;

    public ArrayList<Long> getScaleId() {
        return scaleId;
    }

    public void setScaleId(ArrayList<Long> scaleId) {
        this.scaleId = scaleId;
    }

    public ArrayList<Long> getPatientId() {
        return patientId;
    }

    public void setPatientId(ArrayList<Long> patientId) {
        this.patientId = patientId;
    }
}

/**
 * 量表筛选Controller
 *
 * @author ruoyi
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/dcp/screen")
public class DcpScaleScreenController extends BaseController
{
    @Autowired
    private IDcpScaleScreenService dcpScaleScreenService;
    @Autowired
    private IDcpScaleService dcpScaleService;
    @Autowired
    private IDcpPatientService dcpPatientService;
    @Autowired
    private IDcpResultService dcpResultService;
    /**
     * 查询量表筛选列表
     */
    @PreAuthorize("@ss.hasPermi('dcp:screen:list')")
    @GetMapping("/list")
    public TableDataInfo list(DcpScaleScreen dcpScaleScreen)
    {
        startPage();
        List<DcpScaleScreen> list = dcpScaleScreenService.selectDcpScaleScreenList(dcpScaleScreen);
        return getDataTable(list);
    }

    /**
     * 导出量表筛选列表
     */
    @PreAuthorize("@ss.hasPermi('dcp:screen:export')")
    @Log(title = "量表筛选", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DcpScaleScreen dcpScaleScreen)
    {
        List<DcpScaleScreen> list = dcpScaleScreenService.selectDcpScaleScreenList(dcpScaleScreen);
        ExcelUtil<DcpScaleScreen> util = new ExcelUtil<DcpScaleScreen>(DcpScaleScreen.class);
        util.exportExcel(response, list, "量表筛选数据");
    }

    /**
     * 获取量表筛选详细信息
     */
    @PreAuthorize("@ss.hasPermi('dcp:screen:query')")
    @GetMapping(value = "/{scaleScreenId}")
    public AjaxResult getInfo(@PathVariable("scaleScreenId") Long scaleScreenId)
    {
        return success(dcpScaleScreenService.selectDcpScaleScreenByScaleScreenId(scaleScreenId));
    }

    /**
     * 新增量表筛选
     */
    @PreAuthorize("@ss.hasPermi('dcp:screen:add')")
    @Log(title = "量表筛选", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DcpScaleScreen dcpScaleScreen)
    {
        return toAjax(dcpScaleScreenService.insertDcpScaleScreen(dcpScaleScreen));
    }
    /**
     * 分配 新增量表筛选
     */
    /**
     * 分配 新增量表筛选
     */
    @PreAuthorize("@ss.hasPermi('dcp:screen:add')")
    @Log(title = "量表筛选", businessType = BusinessType.INSERT)
    @PostMapping("/assign")
    public AjaxResult AssignAdd(@RequestBody ScalePatientAssignment assignment) {
        List<Long> scaleIdList = assignment.getScaleId();
        List<Long> patientIdList = assignment.getPatientId();

        // 批量查询患者信息
        Map<Long, DcpPatient> patientMap = new HashMap<>();
        for (Long patientId : patientIdList) {
            DcpPatient patient = dcpPatientService.selectDcpPatientByPatientId(patientId);
            if (patient != null) {
                patientMap.put(patientId, patient);
            }
        }

        for (Long scaleId : scaleIdList) {
            List<DcpScaleScreen> existingScreens = dcpScaleScreenService.selectDcpScaleScreenByScaleId(scaleId);
            DcpScale dcpScale = dcpScaleService.selectDcpScaleByScaleId(scaleId);
            String scaleName = dcpScale.getScaleName();
            // 已经关联的患者 ID 集合
            Set<Long> existingPatientIds = existingScreens.stream()
                    .map(DcpScaleScreen::getPatientId)
                    .collect(Collectors.toSet());
            for (Long patientId : patientIdList) {
                if (existingPatientIds.contains(patientId)) {
                    continue;
                }
                DcpPatient patient = patientMap.get(patientId);
                if (patient != null) {
                    DcpScaleScreen newScreen = new DcpScaleScreen();
                    newScreen.setScaleName(scaleName);
                    newScreen.setScaleId(scaleId);
                    newScreen.setPatientName(patient.getChildName());
                    newScreen.setPatientId(patientId);
                    dcpScaleScreenService.insertDcpScaleScreen(newScreen);
                }
            }
        }
        return toAjax(1);
    }
//    @PreAuthorize("@ss.hasPermi('dcp:screen:add')")
//    @Log(title = "量表筛选", businessType = BusinessType.INSERT)
//    @PostMapping("/assign")
//    public AjaxResult AssignAdd(@RequestBody ScalePatientAssignment assignment)
//    {
//        ArrayList<Long> scaleId = assignment.getScaleId();
//        ArrayList<Long> patientId = assignment.getPatientId();
//        DcpScaleScreen dcpScaleScreen = new DcpScaleScreen();
//
//        for (Long sId : scaleId) {
//            List<DcpScaleScreen> scaleScreen = dcpScaleScreenService.selectDcpScaleScreenByScaleId(sId);
//            DcpScale dcpScale = dcpScaleService.selectDcpScaleByScaleId(sId);
//            String scaleName = dcpScale.getScaleName();
//            dcpScaleScreen.setScaleName(scaleName);
//            dcpScaleScreen.setScaleId(sId);
//            if (!scaleScreen.isEmpty()){
//                for (int i = 0; i < scaleScreen.size(); i++) {
//                    DcpScaleScreen screen = scaleScreen.get(i);
//                    Long p = screen.getPatientId();
//                    for (Long pId : patientId) {
//                        if (p == pId) {
//                            continue;
//                        } else {
//                            DcpPatient dcpPatient = dcpPatientService.selectDcpPatientByPatientId(pId);
//                            String childName = dcpPatient.getChildName();
//                            dcpScaleScreen.setPatientName(childName);
//                            dcpScaleScreen.setPatientId(pId);
//                            dcpScaleScreenService.insertDcpScaleScreen(dcpScaleScreen);
//                        }
//                    }
//                }
//            }else {
//                for (Long pId : patientId) {
//                    DcpPatient dcpPatient = dcpPatientService.selectDcpPatientByPatientId(pId);
//                    String childName = dcpPatient.getChildName();
//                    dcpScaleScreen.setPatientName(childName);
//                    dcpScaleScreen.setPatientId(pId);
//                    dcpScaleScreenService.insertDcpScaleScreen(dcpScaleScreen);
//                }
//            }
//
//
//
//        }
//
//        return toAjax(1);
//    }

//        for (int i = 0; i < scaleId.size(); i++) {
//            Long sId = scaleId.get(i);
//            DcpScale dcpScale = dcpScaleService.selectDcpScaleByScaleId(sId);
//            String scaleName = dcpScale.getScaleName();
//            dcpScaleScreen.setScaleId(sId);
//            dcpScaleScreen.setScaleName(scaleName);
//            for (int j = 0; j < patientId.size(); j++) {
//                Long pId = patientId.get(j);
//                List<DcpScaleScreen> scaleScreen = dcpScaleScreenService.selectDcpScaleScreenByScaleId(sId);
//                if (!scaleScreen.isEmpty()){
//                    for (DcpScaleScreen screen : scaleScreen) {
//                        Long p = screen.getPatientId();
//                        if (p == pId){
//                            continue;
//                        }else {
//                            DcpPatient dcpPatient = dcpPatientService.selectDcpPatientByPatientId(pId);
//                            String childName = dcpPatient.getChildName();
//                            dcpScaleScreen.setPatientId(pId);
//                            dcpScaleScreen.setPatientName(childName);
//                            dcpScaleScreenService.insertDcpScaleScreen(dcpScaleScreen);
//                        }
//                    }
//                }else {
//                    DcpPatient dcpPatient = dcpPatientService.selectDcpPatientByPatientId(pId);
//                    String childName = dcpPatient.getChildName();
//                    dcpScaleScreen.setPatientId(pId);
//                    dcpScaleScreen.setPatientName(childName);
//                    dcpScaleScreenService.insertDcpScaleScreen(dcpScaleScreen);
//                }
//            }
//        }
    /**
     * 修改量表筛选
     */
    @PreAuthorize("@ss.hasPermi('dcp:screen:edit')")
    @Log(title = "量表筛选", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DcpScaleScreen dcpScaleScreen)
    {
        return toAjax(dcpScaleScreenService.updateDcpScaleScreen(dcpScaleScreen));
    }

    /**
     * 删除量表筛选
     */
    @PreAuthorize("@ss.hasPermi('dcp:screen:remove')")
    @Log(title = "量表筛选", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scaleScreenIds}")
    public AjaxResult remove(@PathVariable Long[] scaleScreenIds)
    {
        return toAjax(dcpScaleScreenService.deleteDcpScaleScreenByScaleScreenIds(scaleScreenIds));
    }

    //新增
    @PreAuthorize("@ss.hasPermi('dcp:screen:add')")
    @Log(title = "量表筛选", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult inster(@RequestBody DcpScaleScreen dcpScaleScreen)
    {

        return toAjax(dcpScaleScreenService.addDcpScaleScreen(dcpScaleScreen));
    }
}
